package com.bigger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigger.constant.ErrorConstant;
import com.bigger.context.BaseContext;
import com.bigger.dto.LoginDTO;
import com.bigger.dto.RegisterDTO;
import com.bigger.dto.UpdatePasswordDTO;
import com.bigger.entity.User;
import com.bigger.exception.LoginException;
import com.bigger.exception.PowerException;
import com.bigger.exception.RegisterException;
import com.bigger.mapper.UserMapper;
import com.bigger.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(LoginDTO loginDTO) {
        QueryWrapper<User> wrapper=new QueryWrapper<User>()
                .eq("username",loginDTO.getUsername());
        User user = userMapper.selectOne(wrapper);
        if(user==null){
            throw new LoginException(ErrorConstant.USER_NOT_EXIST);
        }
        String password = user.getPassword();
        if (!DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes(StandardCharsets.UTF_8))
           .equals(password)){
            throw new LoginException(ErrorConstant.PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public void register(RegisterDTO registerDTO) {
        //判断是否是否为[a-z A-Z 0-9]的7-15位账号
        if(!registerDTO.getUsername().matches("\\w{7,15}")){
            throw new RegisterException(ErrorConstant.USERNAME_ERROR);
        }
        if(registerDTO.getPassword().matches("\\w{6,15}")){
            throw new RegisterException(ErrorConstant.PASSWORD_FORMAT_ERROR);
        }
        User user=new User();
        BeanUtils.copyProperties(registerDTO,user);
        user.setPassword(DigestUtils.md5DigestAsHex(registerDTO.getPassword().getBytes(StandardCharsets.UTF_8)));
        user.setId(null);//一定要将id设置为null交给数据库处理（前端是可以穿id字段的防止出现bug）
        user.setIsVIP(User.NOT_VIP);
        userMapper.insert(user);
    }

    @Override
    public void updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        if (!updatePasswordDTO.getNewPassword().matches("\\w{6,15}")){
            throw new LoginException(ErrorConstant.PASSWORD_FORMAT_ERROR);
        }
        QueryWrapper<User> wrapper=new QueryWrapper<User>()
                .eq("username",updatePasswordDTO.getUsername());
        User user = userMapper.selectOne(wrapper);
        if(user==null){
            throw new LoginException(ErrorConstant.USER_NOT_EXIST);
        }
        if(!user.getId().equals(BaseContext.getCurrentId())){
            throw new PowerException(ErrorConstant.INSUFFICIENT_PRIVILEGES);
        }
        String oldPassword=DigestUtils.md5DigestAsHex(updatePasswordDTO.getOldPassword().getBytes(StandardCharsets.UTF_8));
        if(!oldPassword.equals(user.getPassword())){
            throw new LoginException(ErrorConstant.OLD_PASSWORD_ERROR);
        }
        user.setPassword(DigestUtils.md5DigestAsHex(updatePasswordDTO.getNewPassword().getBytes(StandardCharsets.UTF_8)));
        userMapper.updateById(user);
    }

    @Override
    public User getById() {
        User user = userMapper.selectById(BaseContext.getCurrentId());
        return user;
    }
}
